package com.fangyi.yunmusic.controller;

import com.fangyi.yunmusic.exception.YunMusicException;
import com.fangyi.yunmusic.pojo.User;
import com.fangyi.yunmusic.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/user/checkIsArtist")
    public Object checkUserIsArtist(String userId) {
        return userService.checkUserIsArtist(userId);
    }

    @PostMapping("/user/register")
    public Object userRegister(String nickname,String password,String description){
        userService.userRegister(nickname,password,description);
        return "OK";
    }

    @PostMapping("/user/login")
    public Object userLogin(String nickname,String password){
        userService.userLogin(nickname,password);
        return "OK";

    }

    @PostMapping("/user/pwdUpdate")
    public Object pwdUpdate(String userId,String oldPassword,String newPassword){
        if(userService.updatePassword(userId,oldPassword,newPassword)){
            return "OK";
        }
        else throw new YunMusicException("密码输入错误");
    }

    @PostMapping("/user/descUpdate")
    public Object descUpdate(String userId,String description){
        userService.updateDescription(userId,description);
        return "OK";
    }

    @GetMapping("/user/Info")
    public Object getUserInfo(String userId){

        return userService.getUserMessage(userId);
    }

    @PostMapping("/user/verifyName")
    public Object verifyName(String nickname){
        if(userService.verifyUsernameRepetition(nickname))
        return "OK";
        else throw new YunMusicException("用户名已存在");
    }

    @PostMapping(("/user/update"))
    public Object update(User user) {
        userService.updateUser(user);
        return "更新成功";
    }
    @PostMapping("/user/delete")
    public Object delete(String userId){
        userService.deleteUserById(userId);
        return "删除成功";
    }
}
